<template>
  <div>
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      class="mt-dialog"
      width="80%"
      @close="close"
    >

      <div class="top">
        <el-input
          v-model="kwInput"
          placeholder="输入 任意字符 进行筛选"
          class="input-with-select"
          clearable
          @keyup.native.enter="handleSearch"
        >
          <el-button slot="append" icon="el-icon-search" @click="handleSearch">搜索</el-button>
        </el-input>
        <el-tooltip class="item" effect="dark" content="是否查看所有" placement="bottom-start">
          <el-switch v-model="show_all" @change="handleSearch" />
        </el-tooltip>
        <el-tooltip class="item" effect="dark" content="只看自己" placement="bottom-start">
          <el-switch v-model="onlymyself" @change="handleSearch" />
        </el-tooltip>
        <el-button
          v-if="shopeeadsproduct_id !== ''"
          type="primary"
          class="el-icon-plus add-btn"
          :disabled="listLoading"
          @click="handleAction('create', '添加')"
        >添加
        </el-button>
      </div>

      <el-table
        v-loading.fullscreen.lock="listLoading"
        :data="data"
        border
        fit
        highlight-current-row
        style="width: 100%"
        @sort-change="handleSort"
      >
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-form label-position="left" inline class="demo-table-expand">
              <el-form-item label="提醒日期">
                <span>{{ new Date(props.row.alert_at) }}</span>
              </el-form-item>
              <el-form-item label="创建日期">
                <span>{{ props.row.create_at }}</span>
              </el-form-item>
              <el-form-item label="提醒次数">
                <span>{{ props.row.alert_count }}</span>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column align="center" label="ID" prop="id" sortable="custom" width="60" />
        <el-table-column align="center" prop="main_image" label="产品" width="100">
          <template slot-scope="scope">
            <a :href="scope.row.ad_link" target="_blank"><img :src="scope.row.main_image" width="56"></a><br>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作人" prop="owner__last_name" sortable="custom" width="120">
          <template slot-scope="props">
            <span>{{ props.row.owner__first_name + props.row.owner__last_name }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="调整记录" prop="note" sortable="custom">
          <template slot-scope="scope">
            <span>{{ scope.row.note }}</span>
            <el-tag v-if="scope.row.result !== ''" style="display: block" type="success">{{ scope.row.result }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" label="提醒/添加时间" prop="create_at" width="180px">
          <template slot-scope="scope">
            <i class="el-icon-date" style="color:red">{{ scope.row.alert_at_pretty }}</i>
            <i class="el-icon-time">{{ scope.row.create_at_pretty }}</i>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" width="240">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.enable_alert"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="handleEnableTaggle(scope.row)"
            />
            <el-button size="mini" type="success" @click="handleAction('edit', scope.row)">修改</el-button>
            <el-button size="mini" type="danger" @click="handleAction('delete', scope.row)">删除</el-button>
          </template>
        </el-table-column>

      </el-table>

      <el-pagination
        v-if="data.length"
        background
        class="mt-pagination"
        :current-page="page"
        :page-sizes="[10,20,50,100]"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handlePageChange"
      />

    </el-dialog>
    <el-dialog
      append-to-body
      :title="innertitle"
      :width="width"
      :visible.sync="innerDialogVisible"
      :close-on-click-modal="false"
    >
      <el-form :model="temp" label-position="left" label-width="80px">
        <el-form-item v-if="action !== 'delete'" label="调整记录">
          <el-input v-model="temp.note" type="textarea" :rows="4" />
        </el-form-item>
        <el-form-item v-if="action === 'edit'" label="效果反馈">
          <el-input v-model="temp.result" />
        </el-form-item>
        <el-form-item v-if="action !== 'delete'" label="提醒">
          <el-switch
            v-model="temp.enable_alert"
            active-color="#13ce66"
            inactive-color="#ff4949"
          />
        </el-form-item>
        <el-form-item v-if="temp.enable_alert" label="提醒次数">
          <el-input v-model="temp.alert_count" type="number" />
        </el-form-item>
        <el-form-item v-if="temp.enable_alert">
          <el-date-picker
            v-model="temp.alert_at"
            type="datetime"
            value-format="timestamp"
            placeholder="选择提醒时间"
            align="right"
            :picker-options="pickerOptions"
          />
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="innerDialogVisible = false">取 消</el-button>
        <el-button :type="action !== 'delete'?'primary':'danger'" @click="confirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { Get, Post, Put, Delete } from '@/api/shopee/shopeeadsoperationnote'

import qs from 'qs'

const orderMapper = {
  'ascending': 0,
  'descending': 1,
  'null': ''
}
export default {
  name: 'Shopeeadsoperationnote',
  props: ['shopeeadsproduct_id', 'id_product', 'showDialog'],
  data() {
    return {
      title: '虾皮广告产品操作记录',
      data: [],
      listLoading: true,
      kwInput: '',
      show_all: false,
      onlymyself: false,
      order_by: 'alert_at',
      order_desc: 0,
      // pagination
      pagesize: 10,
      page: 1,
      total: 0,
      action: null,
      dialogVisible: false,
      temp: {
        alert_at: '',
        alert_count: 0,
        note: '',
        result: '',
        enable_alert: true
      },
      pickerOptions: {
        shortcuts: [{
          text: '今天',
          onClick(picker) {
            picker.$emit('pick', new Date())
          }
        }, {
          text: '明天',
          onClick(picker) {
            const date = new Date()
            date.setTime(date.getTime() + 3600 * 1000 * 24)
            picker.$emit('pick', date)
          }
        }, {
          text: '3天后',
          onClick(picker) {
            const date = new Date()
            date.setTime(date.getTime() + 3600 * 1000 * 24 * 3)
            picker.$emit('pick', date)
          }
        },
        {
          text: '一周后',
          onClick(picker) {
            const date = new Date()
            date.setTime(date.getTime() + 3600 * 1000 * 24 * 7)
            picker.$emit('pick', date)
          }
        },
        {
          text: '15天后',
          onClick(picker) {
            const date = new Date()
            date.setTime(date.getTime() + 3600 * 1000 * 24 * 15)
            picker.$emit('pick', date)
          }
        },
        {
          text: '一个月后',
          onClick(picker) {
            const date = new Date()
            date.setTime(date.getTime() + 3600 * 1000 * 24 * 30)
            picker.$emit('pick', date)
          }
        }
        ]
      },
      width: '80%',
      innertitle: '',
      innerDialogVisible: false
    }
  },
  watch: {
    shopeeadsproduct_id(v) {
      if (v) {
        this.order_by = ''
        this.order_desc = ''
      } else {
        this.order_by = 'alert_at'
        this.order_desc = 0
      }
    },
    showDialog(v) {
      this.dialogVisible = v
      if (v) {
        this.title = this.id_product + '广告产品操作记录'
        this.getList()
      }
    }
  },
  mounted() {
    if (this.id_product !== '') {
      this.order_by = ''
      this.order_desc = ''
    } else {
      this.order_by = 'alert_at'
      this.order_desc = 0
    }
  },
  methods: {
    resetTemp() {
    },
    getList() {
      this.listLoading = true
      Get(null, qs.stringify({
        kw: this.kwInput,
        show_all: this.show_all,
        onlymyself: this.onlymyself,
        shopeeadsproduct_id: this.shopeeadsproduct_id,
        id_product: this.id_product,
        pagesize: this.pagesize,
        page: this.page,
        order_by: this.order_by,
        order_desc: this.order_desc
      }))
        .then(response => {
          this.scrollTop()
          this.data = response.data.data
          this.listLoading = false
          this.total = response.data.total
        }).catch(err => {
          console.log(err)
          this.listLoading = false
        })
    },
    handleEnableTaggle(row) {
      if (row.enable_alert) {
        this.handleAction('edit', row)
      } else {
        this.temp = Object.assign({}, row)
        this.temp.alert_at = new Date(this.temp.alert_at)
        this.action = 'edit'
        this.confirm()
      }
    },
    handleAction(action, row) {
      this.action = action
      switch (action) {
        case 'create':
          this.width = '50%'
          this.innertitle = `${this.id_product} 添加新记录`
          this.innerDialogVisible = true
          break
        case 'delete':
          this.width = '30%'
          this.innertitle = `确认要删除 ${row.id_product} 记录?`
          this.temp = Object.assign({}, row)
          this.innerDialogVisible = true
          break
        case 'edit':
          this.width = '50%'
          this.innertitle = `修改 ${row.id_product} 记录:${row.id}`
          this.temp = Object.assign({}, row)
          this.temp.alert_at = new Date(this.temp.alert_at)
          this.innerDialogVisible = true
          break
      }
    },
    confirm() {
      this.temp.shopeeadsproduct_id = this.shopeeadsproduct_id
      const _data = this.temp
      _data['alert_at'] = Number(_data['alert_at'])
      switch (this.action) {
        case 'create':
          if (_data.note === '') {
            alert('note必填')
            return
          }
          _data.website_id = this.website_id
          Post(_data
          ).then(response => {
            this.innerDialogVisible = false
            this.getList()
            this.$emit('reload')
            this.notify('添加成功')
          })
          break
        case
          'delete'
          :
          Delete(_data.id).then((response) => {
            this.innerDialogVisible = false
            this.getList()
            this.$emit('reload')
            this.notify('删除成功')
          })
          break
        case 'edit':
          Put(_data.id, _data).then((response) => {
            this.innerDialogVisible = false
            this.$emit('reload')
            this.getList()
            this.notify('更新成功')
          })
          break
      }
    },
    notify(message, duration) {
      this.$notify({
        title: '成功',
        message: message,
        type: 'success',
        duration: duration || 2000
      })
    },
    // pageSize 改变后触发 table 更新
    handleSizeChange(val) {
      this.pagesize = val
      this.getList()
    },
    // page 改变后触发 table 更新
    handlePageChange(val) {
      this.page = val
      this.getList()
    },
    // 点击搜索
    handleSearch() {
      this.page = 1
      this.getList()
    },
    handleSort(v) {
      this.order_by = v.prop
      this.order_desc = orderMapper[v.order]
      this.getList()
    },
    scrollTop() {
      window.scrollTo(0, 0)
    },
    close() {
      this.$emit('closeMyDialog', false)
    }
  }
}
</script>

<style scoped>
  .el-tag + .el-tag {
    margin-left: 10px;
  }

  .top >>> .el-input {
    width: auto;
  }

  .top >>> .input-with-select .el-input-group__prepend {
    background-color: #fff;
  }

  .top {
    padding-left: 20px;
    margin-bottom: 5px;
    margin-top: 28px;
  }

  /*.input-with-select {*/
  /*width: calc(100% - 200px);*/
  /*}*/

  .add-btn {
    float: right;
    margin-right: 20px;
    width: 150px
  }

  .mt-pagination {
    float: right;
    margin-top: 5px;
  }

  .app-container >>> .el-dialog {
    margin-top: 48px !important;
    margin-bottom: 5px;
  }

  .mt-dialog >>> .el-transfer-panel {
    width: calc(50% - 60px);
  }

  .mt-form >>> .el-select {
    width: 100%;
  }

  .mt-form >>> .el-input__inner {
    width: 100%;
  }

  .mt-dialog >>> .el-dialog {
    margin: 10px auto !important;
  }

  .mt-dialog >>> .el-dialog__body {
    padding-bottom: 60px;
    padding-top: 5px;
  }

  .top >>> .el-select {
    width: 300px;
  }

  .mt-form >>> a:visited {
    color: purple;
  }

  .mt-form >>> a:link {
    color: #409EFF;
  }

  .mt-form >>> a:active {
    color: red;
  }

  .mt-form >>> a:hover {
    color: red;
    text-decoration: underline;
  }
</style>
